/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_SSAP_CLIENT_IMPL_H
#define SLE_SSAP_CLIENT_IMPL_H

#include "sle_utils_observer_list.h"
#include "i_sle_ssap_client.h"
#include "sle_ssap_client_interface.h"
#include "sle_ssap_client_callback_stub.h"
#include "sle_ssap_client_parcel.h"
#include "sle_ssap_common_parcel.h"
#include "sle_host_interface.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
class SleSsapClient::SleSsapClientImpl {
public:
    SleSsapClientImpl(const SleUuid &uuid);
    ~SleSsapClientImpl();
    SleErrorCode CheckOperation();

    class SleSsapClientCallbackStubImpl;
    sptr<SleSsapClientCallbackStubImpl> callbackImpl_ = nullptr;

    uint8_t clientId_;
    Uuid clientUuid_;
    bool isRegistered_;
    sptr<ISleSsapClient> proxy_ = nullptr;
    sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
    SleObserverList<SleSsapClientCallback> callbacks_;
    std::mutex mutexProxy_;

private:
    void GetProxy();
    void ResetService(const wptr<IRemoteObject> &remote);
};

class SleSsapClient::SleSsapClientImpl::SleSsapClientCallbackStubImpl : public SleSsapClientCallbackStub {
public:
    SleSsapClientCallbackStubImpl(SleSsapClient::SleSsapClientImpl &impl) : impl_(impl) {}
    ~SleSsapClientCallbackStubImpl() = default;

    void OnFindService(uint8_t clientId, uint16_t connectId,
        const SsapClientFindServiceRes &result, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientFindServiceRes ret;
        result.ConvertToClientFindServiceRes(ret);
        impl_.callbacks_.ForEach([connectId, &ret, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnFindService(connectId, ret, (SleErrorCode)errCode);
        });
    }

    void OnFindProperty(uint8_t clientId, uint16_t connectId,
        const SsapClientFindPropertyRes &result, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientFindPropertyRes ret;
        result.ConvertToClientFindPropertyRes(ret);
        impl_.callbacks_.ForEach([connectId, &ret, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnFindProperty(connectId, ret, (SleErrorCode)errCode);
        });
    }

    void OnFindComplete(uint8_t clientId, uint16_t connectId,
        uint8_t type, const Uuid &uuid, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientFindStructureRes ret = {
            .type = (SsapFindType)type,
            .uuid = uuid.ConvertToSleUuid()
        };
        impl_.callbacks_.ForEach([connectId, &ret, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnFindComplete(connectId, ret, (SleErrorCode)errCode);
        });
    }

    void OnWriteResult(uint8_t clientId, uint16_t connectId, uint16_t handle, uint8_t type, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientWriteRes ret = {
            .handle = handle,
            .type = (SleSsapPropertyType) type
        };
        impl_.callbacks_.ForEach([connectId, &ret, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnWriteResult(connectId, ret, (SleErrorCode)errCode);
        });
    }

    void OnReadResult(uint8_t clientId, uint16_t connectId,
        const SsapClientHandleValue &value, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientHandleValue data;
        value.ConvertToClientHandleValue(data);
        impl_.callbacks_.ForEach([connectId, &data, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnReadResult(connectId, data, (SleErrorCode)errCode);
        });
    }

    void OnReadByUuidComplete(uint8_t clientId, uint16_t connectId,
        uint8_t type, const Uuid &uuid, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientReadByUuidCmpRes ret = {
            .uuid = uuid.ConvertToSleUuid(),
            .type = (SleSsapPropertyType)type
        };
        impl_.callbacks_.ForEach([connectId, &ret, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnReadByUuidComplete(connectId, ret, (SleErrorCode)errCode);
        });
    }

    void OnExchangeInfo(uint8_t clientId, uint16_t connectId,
        uint32_t mtu, uint16_t version, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        SsapMtuInfo info = {
            .mtuSize = mtu,
            .version = version
        };
        impl_.callbacks_.ForEach([connectId, &info, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnExchangeInfo(connectId, info, (SleErrorCode)errCode);
        });
    }

    void OnNotification(uint8_t clientId, uint16_t connectId,
        const SsapClientHandleValue &value, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientHandleValue data;
        value.ConvertToClientHandleValue(data);
        impl_.callbacks_.ForEach([connectId, &data, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnNotification(connectId, data, (SleErrorCode)errCode);
        });
    }

    void OnIndication(uint8_t clientId, uint16_t connectId,
        const SsapClientHandleValue &value, uint32_t errCode) override
    {
        // if (impl_.clientId_ != clientId) {
        //     return;
        // }
        ClientHandleValue data;
        value.ConvertToClientHandleValue(data);
        impl_.callbacks_.ForEach([connectId, &data, errCode](std::shared_ptr<SleSsapClientCallback> callback) {
            callback->OnIndication(connectId, data, (SleErrorCode)errCode);
        });
    }

private:
    SleSsapClient::SleSsapClientImpl &impl_;
    SLE_DISALLOW_COPY_AND_ASSIGN(SleSsapClientCallbackStubImpl);
};
} // SleStandard
} // NearLink
} // OHOS

#endif //SLE_SSAP_CLIENT_IMPL_H